package com.mystore.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mystore.common.paging.StylePage;
import com.mystore.framework.pojo.User;
import com.mystore.framework.service.impl.IRoleServiceImpl;
import com.mystore.framework.service.impl.IUserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

@Controller
@RequestMapping("manage/user")
public class UserController {
    @Resource
    private IUserServiceImpl iUserServiceImpl;
    @Resource
    private IRoleServiceImpl iRoleServiceImpl;
    @GetMapping("/page.do")
    public String page(@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1")long current,@RequestParam(defaultValue = "10")long size,String result) {
        IPage<User> userList = iUserServiceImpl.selectPageByData(data,new Page<>(current,size));
        model.addAttribute("users", new StylePage<>(userList));
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "user_page";
    }
    @GetMapping("/insert.do")
    public String insert(Model model) {
        model.addAttribute("roles",iRoleServiceImpl.selectAll());
        model.addAttribute("mode","insert");
        return "user_info";
    }
    @GetMapping("/update.do")
    public String update(Model model,int userId) {
        model.addAttribute("roles",iRoleServiceImpl.selectAll());
        model.addAttribute("user",iUserServiceImpl.selectById(userId));
        model.addAttribute("mode","update");
        return "user_info";
    }
    @PostMapping("/save.do")
    public String save(RedirectAttributes model, User user, String mode) {
        try {
            if (mode.equals("insert"))
                model.addAttribute("result",iUserServiceImpl.insertOne(user)>0);
            else {
                if(user==null) {
                    model.addAttribute("result", false);
                    return "redirect:/manage/user/page.do";
                }
                User myUser=iUserServiceImpl.selectById(user.getUserId());
                myUser.setUserPassword(!StringUtils.hasText(user.getUserPassword())? myUser.getUserPassword():user.getUserPassword());
                myUser.setUserNewPassword(!StringUtils.hasText(user.getUserNewPassword()) ? myUser.getUserPassword():user.getUserNewPassword());
                model.addAttribute("result",iUserServiceImpl.updateOne(user));
            }
        }catch (RuntimeException e) {
            model.addAttribute("result",false);
        }
        return "redirect:/manage/user/page.do";
    }
    @GetMapping("/delete.do")
    public String delete(RedirectAttributes model,int userId) {
        model.addAttribute("result",iUserServiceImpl.deleteById(userId));
        return "redirect:/manage/user/page.do";
    }
}

